package edu.stanford.cs.cs294s;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LocationForm extends Activity{
	private EditText newFact;
	private int pointIndex;
	private ServerConnection sc; 
	private Context mContext;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simpleedit);
        
        mContext = this;  
        sc = new ServerConnection("http://colby.stanford.edu/cs294s/location/set.php");
        newFact = (EditText)findViewById(R.id.newfactText);
        String index = this.getIntent().getExtras().getString("index");
        pointIndex = Integer.parseInt(index);
        
        
        Button submit = (Button)findViewById(R.id.submitButton);
        submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				boolean isSuccess = false;
				if(newFact.getText().length() > 5)
					 isSuccess = sc.addFact(pointIndex, newFact.getText().toString());
				
				System.err.println("THE Value of isSuccess is : " + isSuccess);
				if(isSuccess){
					Toast.makeText(mContext, "New fact added", Toast.LENGTH_SHORT).show();
					setResult(100);
					finish();
				}
				else{
					Toast.makeText(mContext, "Operation failed", Toast.LENGTH_SHORT).show();
				}
			}
		});
    }
}
